package com.java8.stream.dome;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test4 {
    public static void master() {
        System.out.println("-----------------Test4.master()-----------------");

        //limit()
        Integer[] limitNums = {100, 30, 400, 200, 600, 20, 800};
        Integer[] newLimitNums1 = Arrays.stream(limitNums).filter(n -> n > 100).toArray(Integer[]::new);
        Integer[] newLimitNums2 = Arrays.stream(limitNums).filter(n -> n > 100).limit(3).toArray(Integer[]::new);
        //结果：newNums= [400, 200, 600]
        System.out.println("原始：\n" + Arrays.toString(limitNums));
        System.out.println("筛选：\n" + Arrays.toString(newLimitNums1));
        System.out.println("limit()：\n" + Arrays.toString(newLimitNums2));

        //skip();用于跳过前面n个元素
        Integer[] skipNums = {100, 30, 400, 200, 600, 20, 800};
        Integer[] newSkipNums = Arrays.stream(skipNums).skip(2).toArray(Integer[]::new);
        //结果：newNums= [400, 200, 600, 20, 800]
        System.out.println("原始：\n" + Arrays.toString(skipNums));
        System.out.println("skip()：\n" + Arrays.toString(newSkipNums));

        //map();
        Integer[] nums3 = {2, 3, 5, 6, 7, 9, 8};
        Integer[] newNums3 = Arrays.stream(nums3).map(n -> n * n).toArray(Integer[]::new);
        //结果：newNums= [4, 9, 25, 36, 49, 81, 64]
        System.out.println("原始：\n" + Arrays.toString(nums3));
        System.out.println("map()：\n" + Arrays.toString(newNums3));

        /**获取单词，并且去重**/
        List<String> list = Arrays.asList("hello welcome", "world hello", "hello world", "hello world welcome");

        System.out.println("原始：\n" + list);

        System.out.println("直接输出---------- ");
        //map和flatmap的区别
        list.stream().map(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("---------- ");
        list.stream().flatMap(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList()).forEach(System.out::println);

        System.out.println("实际返回值---------- ");
        //实际上返回的类似是不同的
        List<Stream<String>> listResult = list.stream().map(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());
        List<String> listResult2 = list.stream().flatMap(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());
        System.out.println("map：\n" + listResult);
        System.out.println("flatmap：\n" + listResult2);
    }


}
